package com.sky.myapplication.kotlin.kotlinActivity

import android.graphics.Canvas
import android.os.Bundle
import android.os.Handler
import android.view.View
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.widget.NestedScrollView
import androidx.recyclerview.widget.*
import com.sky.myapplication.R
import com.sky.myapplication.kotlin.data.ListBean
import com.sky.myapplication.kotlin.kotlinAdapter.RecycleAdapter
import com.sky.myapplication.kotlin.kotlinInterface.ItemClickKotlinInterface
import com.sky.myapplication.kotlin.weight.MyItemDecoration

/**
 * @Author: Minxp
 * @UpdateDate: 2021/7/3 0003
 * @Description: java类作用描述
 */
class RecycleViewKotlinActivity : AppCompatActivity(), ItemClickKotlinInterface {
    private val LINEAR_LAYOUT_TYPE = 1
    private val GRIDE_LAYOUT_TYPE = 2
    private val STAGGERED_GRID_LAYOUT = 3

    var layoutType: Int = 1

    lateinit var rv_list: RecyclerView
    lateinit var btn_one: Button
    lateinit var btn_two: Button
    lateinit var btn_three: Button
    lateinit var nsv_body: NestedScrollView

    lateinit var adapter: RecycleAdapter

    var data: ArrayList<ListBean>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycle_view)
//        var handler = Handler()
        //handler.sendMessage ->

        initView()
        initRecycleView()
//        nsv_body.pageScroll(View.FOCUS_DOWN)
        nsv_body.fling(0)
        nsv_body.smoothScrollTo(0, 0)
    }

    fun initView() {
        nsv_body = findViewById(R.id.nsv_body)
        rv_list = findViewById(R.id.rv_list)
        btn_one = findViewById(R.id.btn_one)
        btn_two = findViewById(R.id.btn_two)
        btn_three = findViewById(R.id.btn_three)

        btn_one.setOnClickListener {
            if (layoutType == LINEAR_LAYOUT_TYPE) {
                layoutType = GRIDE_LAYOUT_TYPE
                rv_list.layoutManager = GridLayoutManager(this, 3)
            } else if (layoutType ==  GRIDE_LAYOUT_TYPE) {
                layoutType = STAGGERED_GRID_LAYOUT
                rv_list.layoutManager = StaggeredGridLayoutManager(3, RecyclerView.VERTICAL)
            } else {
                layoutType = LINEAR_LAYOUT_TYPE
                rv_list.layoutManager = LinearLayoutManager(this)
            }
            rv_list.adapter?.notifyDataSetChanged()
        }

        btn_two.setOnClickListener{
            data!!.add(ListBean("111"))
            adapter.notifyDataSetChanged()
        }

        btn_three.setOnClickListener {
            var divider = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
            divider.setDrawable(resources.getDrawable(R.drawable.divider))
            rv_list.addItemDecoration(divider)

//            if(rv_list.itemDecorationCount > 0){
//                var itemDecoration: RecyclerView.ItemDecoration = rv_list.getItemDecorationAt(0)
//                if (itemDecoration == null) {
//                    rv_list.addItemDecoration(divider)
//                }
//            } else {
//                rv_list.addItemDecoration(divider)
//            }

//            rv_list.adapter?.notifyDataSetChanged()

//            rv_list.addItemDecoration(object : RecyclerView.ItemDecoration(){
//                override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
//                    super.onDraw(c, parent, state)
//                }
//            })
        }

        btn_three.setOnClickListener {
            nsv_body.scrollTo(0, 0)

        }
    }

    private fun initRecycleView(){
        rv_list.layoutManager = LinearLayoutManager(this)
        rv_list.addItemDecoration(MyItemDecoration())
//        rv_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))

//        rv_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
//        if(rv_list.itemDecorationCount > 0){
//            var itemDecoration = rv_list.getItemDecorationAt(0)
//            if (itemDecoration == null) {
//                rv_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
//            }
//        } else {
//            rv_list.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
//        }

        data = ArrayList<ListBean>()
        for (index in 1..3){
            data!!.add(ListBean("Tom"))
            data!!.add(ListBean("Rose"))
            data!!.add(ListBean("Ben"))
        }
        adapter = RecycleAdapter(this, data!!)
        adapter.setOnItemClickListener(this)
        rv_list.adapter = adapter
    }

    override fun onItemClick(postion: Int) {
        data!![postion].isSelect = !data!![postion].isSelect
        adapter.notifyDataSetChanged()
//        Toast.makeText(this, data?.get(postion), Toast.LENGTH_SHORT).show()
    }

    override fun onItemLongClick(postion: Int) {
        super.onItemLongClick(postion)
    }
}